package com.ultra.template.parse;


import com.ultra.enums.ArgumentTypeEnum;
import com.ultra.exception.UnableResolveTypeException;
import com.ultra.exception.UnknownTypeException;
import com.ultra.reflect.ReflectUtil;
import com.ultra.template.content.Argument;
import com.ultra.template.content.Pairs;

import java.lang.reflect.GenericArrayType;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;




/**
 * @author yinger
 * @description TODO
 * @date 2024/12/30
 **/
public final class GenericArrayParser {
    public static void _fromGenericArray(Argument config) throws UnableResolveTypeException, UnknownTypeException, ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, UnknownTypeException {
        config.setIThCollection(Parser.times[config.getDepth()]++);
        config.setArgumentType(ArgumentTypeEnum.ARRAY);
        Type type = config.getType();

        String fullClassNameStr = ReflectUtil.getGenericArraySignatureByType(type);
        config.setClassName(fullClassNameStr);

        if (config.getChild() == null || config.getChild().length == 0) {
            config.setChild(new Argument[]{new Argument()});
        }
        config.getChild()[0].setType(((GenericArrayType) type).getGenericComponentType());
        config.getChild()[0].setDepth(config.getDepth() + 1);
        config.getChild()[0].setIThArgument(config.getIThArgument());
        Parser.flatteningMap.put(new Pairs(config.getIThArgument(),config.getDepth(),config.getIThCollection()),config);
        Parser.parserDispatcher(config.getChild()[0]);
    }
}
